import MyIcon from '@/components/MyIcon';
import ScrollBarLayoutNotScroll from '@/components/ScrollBarLayoutNotScroll';
import { getPersonalInfo, getTenantID, getUserID } from '@/utils';
import { Col, Row } from 'antd';
import { useEffect, useState } from 'react';
import { history } from 'umi';
import Cls from 'classnames';

export const CommonFunctionTable = ({ curRes }) => {
  const styleFixCls = 'common-function-table';
  const [menuList, setMenuList] = useState<Record<string, any>[]>([]);
  const filterContrac = [
    '/contractTemplateController/manage',
    '/contractController/draftManage',
    '/contractController/carefulManage',
    '/contractController/performManage',
    '/contractController/oversightManage',
    '/contractController/allSearchManage',
  ];

  useEffect(() => {
    if (curRes && JSON.stringify(curRes) != '{}' && getUserID()) {
      const { paramObject } = getPersonalInfo();
      if (paramObject) {
        if (paramObject?.menuResourceList?.length) {
          const contractManage = paramObject?.menuResourceList?.find((item) => item?.serviceType == `${getTenantID()}00001013`)?.children;
          if (contractManage?.length) {
            const saveMenu = contractManage?.filter((item) => {
              if (filterContrac?.indexOf(item?.url) != -1 && curRes?.[item?.url]) {
                return item;
              }
            });
            setMenuList(saveMenu);
          }
        }
      }
    }
  }, [curRes, getUserID()]);

  return (
    <div className={styleFixCls}>
      <ScrollBarLayoutNotScroll>
        <Row gutter={[16, 16]} className={`${styleFixCls}-menuDivBox`}>
          {menuList?.map((item: any, index: number) => {
            return (
              <Col
                span={8}
                key={`homePageContract-menuItem-${index}`}
                onClick={() => {
                  history?.push({
                    pathname: item?.url,
                  });
                }}
              >
                <div className={Cls(`${styleFixCls}-menuDivBox-iconDiv`)}>
                  <MyIcon type={item?.icon} isTheme={true} style={{ fontSize: 34 }} />
                </div>
                <div style={{ fontSize: 13, marginTop: 5, whiteSpace: 'nowrap', fontWeight: 600 }}>{item?.name || '--'}</div>
              </Col>
            );
          })}
        </Row>
      </ScrollBarLayoutNotScroll>
    </div>
  );
};
